package cn.com.yang.server.jetty;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

/**
 * 多context处理
 * @author yangyijin
 * @since 2023年5月18日-下午6:34:43
 */
public class JettyMultiContext {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8081);
		//第一个context
		ContextHandler context = new ContextHandler("/");
		context.setContextPath("/");
		context.setHandler(new JettyHelloHandler("ROOT"));
		
		//第二个context
		ContextHandler secd = new ContextHandler("/api");
		context.setHandler(new JettyHelloHandler("test"));
		
		//第三个context
		ContextHandler third = new ContextHandler("/");
		third.setVirtualHosts(new String[] {"127.0.0.2"});
		third.setHandler(new JettyHelloHandler("hello"));
		
		//第四个context
		ContextHandler fourth = new ContextHandler("/API/hello.do");
		fourth.setHandler(new JettyHelloHandler("我是","我是报文体"));
		
		//context集合
		ContextHandlerCollection collections = new ContextHandlerCollection();
		collections.setHandlers(new Handler[] {context,secd,third,fourth});
		server.setHandler(collections);
		server.start();
		server.join();
	}
}
